#!/bin/bash
# $Id$
#
# Description: Generate network stats for screen
#
# Copyright (C) 2003,2004,2005,2006 Charles Mauch <cmauch@gmail.com>
#
# Verbatim copying, distribution, and display of this program is permitted in
# any medium, provided this notice is preserved. Alternatively, you may create
# derivative works of any sort for any purpose, provided your versions contain
# no attribution to me, and that you assert your own authorship (and not mine)
# in every practical medium.

# netinfo <delay> <interface>

ipaddr=`/sbin/ifconfig $2 | awk '/inet addr/{ split($2,s,/:/); print s[2]; }' `

prefix="$2{+b dk}/{-b dw}$ipaddr{+b dk}"

last_rx=0
last_tx=0
while true; do
   stats=`cat /proc/net/dev | grep $2 | tr : \ `
   rx=` echo $stats | cut -d\  -f2 `
   tx=` echo $stats | cut -d\  -f10 `

   up=$(( ( $tx - $last_tx ) / $1 ))
   dn=$(( ( $rx - $last_rx ) / $1 ))

   # note: 8/1024 == 128 >> bytes to kilobits

   echo -n "$prefix "
   #if [ $last_tx -gt 0 && $last_rx -gt 0 ]; then
       echo -n 
   #fi
   #else
       echo "$up $dn" | awk '{printf "{-b dc}%06.2f{+b dw}:{-b dc}%06.2f\n",$1/128,$2/128}'
   #fi

   last_tx=$tx
   last_rx=$rx
   sleep $1
done
